<?php
class Application_Model_GameObjects extends Xend_Db_Model {
	protected $_dbTableName = 'Application_Model_Table_GameObjects';

	public function __construct() {
		parent::__construct();
	}

	/**
	 * speichert einen Datensatz vom Typ Application_Model_Schema_GameObject
	 * @param object Application_Model_Schema_GameObject
	 * @return int ID des Datensatzes in der Datenbanktabelle game_objects
	 */
	public function save(Application_Model_Schema_GameObject $gameObject) {
		$ret	= null;
		$data = array(
			'objects_id'	=> $gameObject->object->id
		);

		if($gameObject->parent instanceof Application_Model_Schema_Object) {
			$data['parent_objects_id'] = $gameObject->parent->id;
		}

		if($gameObject->enabled === true) {
			$data['enabled'] = 1;
		}

		if($gameObject->id === null) {
			$ret = $this->getDbTable()->insert($data);

		} else {
			$ret = $this->getDbTable()->update($data, array('id = ?' => $gameObject->id));
		}

		return (int) $ret;
	}
	

	/**
	 * fragt eine GameObject Eintrag ab, der auf die Bedingungen zutrifft
	 * @param array $where Array mit Bedingungen, die an die Datenbank uebergeben werden
	 * @param array $ref Array mit Tabellen, die verknuepft werden sollen
	 * @return array Array mit den Daten, die aus der Datenbank geladen wurden
	 */
	public function find($where = array(), $ref = array()) {
		$gameObject = null;

		if(is_array($data = $this->findall($where, $ref, array('limit' => 1)))) {
			$gameObject = current($data);
		}

		return $gameObject;
	}	

	/**
	 * liesst alle Objekte aus, die in der Tabelle game_objects eingetragen wurden und
	 * den Bedingungen entsprechen
	 * @param array $where Array mit Bedingungen, die an die Datenbank uebergeben werden
	 * @param array $ref Array mit Tabellen, die verknuepft werden sollen
	 * @param array $options Optionen, die waerhend der Abfrage verarbeitet werden
	 * @return array Array mit den Daten, die aus der Datenbank geladen wurden
	 */
	public function findall($where = array(), $ref = array(), $options = array()) {
		$gameObjects	= array();
		$ref					= array_flip($ref);
		$select				= $this->_database
			->select()
			->from(
				array('game_objects'),
        array('game_objects.id', 'game_objects.objects_id', 'game_objects.parent_objects_id', 'game_objects.enabled')
			);
		
		// Referenzen
		// -----------------------------------------------------------------------------------
		if(array_key_exists('Objects', $ref)) {
			$select
				->joinLeft('objects', 'game_objects.objects_id = objects.id', null)
				->columns(array('id', 'hid', 'object_types_id', 'name'), 'objects');
		}		

		// Where
		// -----------------------------------------------------------------------------------
		if(is_array($where) && !empty($where)) {
			foreach($where as $k => $v) {
				$select->where($k, $v);
			}
		}
		
		// Options
		// -----------------------------------------------------------------------------------
		if(isset($options['limit'])) {
			$select->limit($options['limit'], 0);
		}		

		// Datenauswertung
		// -----------------------------------------------------------------------------------
		foreach($this->fetch($select) as $k => $v) {
			$dGameObject	= new Application_Model_Schema_GameObject();

			// ---------------------------------------------------------------------------------
			// Object
			$dObject = new Application_Model_Schema_Object();
			$dObject->setId($v['game_objects']['objects_id']);
			
			if(array_key_exists('Objects', $ref)) {
				$dObjectType = new Application_Model_Schema_ObjectType();
				$dObjectType->setId($v['objects']['object_types_id']);
				
				$dObject
					->setHid($v['objects']['hid'])
					->setType($dObjectType)
					->setName($v['objects']['name']);
			}				

			// ---------------------------------------------------------------------------------
			// Zuordnung
			$dGameObject
				->setId($v['game_objects']['id'])
				->setEnabled($v['game_objects']['enabled'])
				->setObject($dObject);

			$gameObjects[(int) $v['game_objects']['id']] = $dGameObject;
		}
		
		if(empty($gameObjects) === false) {
			
			// Actions
			// ---------------------------------------------------------------------------------
			if(array_key_exists('Actions', $ref)) {
				$objects = $this->_assignActionReferences($gameObjects);
			}				
		}

		return $gameObjects;
	}
	
	/**
	 * verknuepft die, per Array uebergebenen, Objekte mit den Daten der
	 * referenzierten Actions (action_use_objects)
	 * @param array $sources Array mit den Daten der Objekte
	 * @param array $references optionales Array mit den UID der Objekte
	 * @return array Array der Objekte mit den verknuepften Actions-Eintraegen
	 */
	protected function _assignActionReferences($sources, $references = array()) {
		if(empty($references)) {
			foreach($sources as $source) {
				$references[$source->object->id] = $source->id;
			}
		}
		
		$select = $this->_database
			->select()
			->from(
				array('actions'),
        array('actions.id', 'actions.name', 'action_use_objects.objects_id')
			)
			->join('action_use_objects', 'actions.id = action_use_objects.actions_id', null)
			->where('action_use_objects.objects_id IN (?)', array_keys($references));

		// Datenauswertung
		// -----------------------------------------------------------------------------------
		foreach($this->fetch($select) as $k => $v) {
			$objectId = (int) $v['action_use_objects']['objects_id'];

			if(array_key_exists($objectId, $references)) {
				$action = new Application_Model_Schema_Action();
				$action
					->setId($v['actions']['id'])
					->setName($v['actions']['name']);

				$sources[$references[$objectId]]->object->setAction($action, $action->id);
			}
		}

		return $sources;
	}		
}
?>